package bawei.com.fragment;

import android.os.Bundle;



import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;



import android.support.v4.widget.SwipeRefreshLayout;

import android.support.v7.widget.GridLayoutManager;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;


import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import com.google.gson.Gson;
import com.squareup.okhttp.Request;


import com.squareup.okhttp.Response;


import com.squareup.okhttp.ResponseBody;


import bawei.com.adapter.SopCastAdapter;


import bawei.com.bilibili.MyAppliction;
import bawei.com.bilibili.R;


import bawei.com.model.BaseCallback;


import bawei.com.model.NetWorkUtil;
import bawei.com.model.Network;
import bawei.com.model.OkHttpHelper;


import bawei.com.model.bean.SopCastBean;


/**
 * 类描述:直播
 * 作者：cwm
 * 时间:2017/3/9 15:49
 * 邮箱:18310832074@163.com
 */

public
class
SopCastFragment extends Fragment {


    private View view;


    private RecyclerView recyclerView;


    private SopCastAdapter sopCastAdapter;
    SopCastBean.DataBean data=new SopCastBean.DataBean();
    SopCastBean sopCastBean=new SopCastBean();

    private SwipeRefreshLayout swipeRefreshLayout;
    public static boolean flag=true;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.sopcastfragment, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycle);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getRefresh();
        getbanner();
        /*if(NetWorkUtil.isNET()){
            getbanner();
        }else {
            //String json1 = MyAppliction.aCache.getAsString("json"+1);
           // SopCastBean sopCastBean1 = new Gson().fromJson(json1, SopCastBean.class);
            if(sopCastAdapter!=null){

                setAdapter(this.sopCastBean);
            }else {

                SopCastBean.DataBean   data  = sopCastBean1.getData();

                Log.e("TAG", "onActivityCreated:-- "+data.getPartitions().size());
                GridLayoutManager layout = new GridLayoutManager(getActivity(), 12);
                layout.setOrientation(LinearLayoutManager.VERTICAL);
                layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

                    @Override
                    public int getSpanSize(int position) {

                        return sopCastAdapter.getSpanSize(position);
                    }
                });

                recyclerView.setLayoutManager(layout);
                sopCastAdapter = new SopCastAdapter(getActivity(), data);
                recyclerView.setAdapter(sopCastAdapter);
            }
        }
*/
    }

    public void  getRefresh(){

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 停止刷新
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 1000); // 2秒后发送消息，停止刷新
            }
        });
        // 设置下拉圆圈上的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.pink_text_color);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setDistanceToTriggerSync(400);
        swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
    }

    //请求轮播图数据
    private void getbanner() {


        OkHttpHelper.getinstance().get("http://live.bilibili.com/" + Network.HOME_DIRECT, new BaseCallback<SopCastBean>() {
            @Override
            public void onRequestBefore() {
            }
            @Override
            public void onFailure(Request request, Exception e) {
            }
            @Override
            public void onSuccess(Response response, SopCastBean sopCastBean) {

                setAdapter(sopCastBean);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
            }
        });
       // OkHttpHelper


    }

    public void setAdapter(SopCastBean sopCastBean){
        GridLayoutManager layout = new GridLayoutManager(getActivity(), 12);
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {

                return sopCastAdapter.getSpanSize(position);
            }
        });

        recyclerView.setLayoutManager(layout);
        sopCastAdapter = new SopCastAdapter(getActivity(),sopCastBean.getData());
        recyclerView.setAdapter(sopCastAdapter);

    }




}
